public class Main {
    //leetcode水果成篮问题
    public int totalFruit(int[] fruits){
        int n =fruits.length;
        int[] hash = new int[n+1];
        int count = 0;
        int ret = 0;//结果
        int L = 0;
        int R = 0;
        while(R<n){
            //进窗口
            int in = fruits[R];
            hash[in]++;
            if(hash[in]==1) count++;
            //出窗口
            while(count>2){
                int out = fruits[L];
                hash[out]--;
                if(hash[out]==0) count--;
                L++;
            }
            //更新结果
            ret = Math.max(ret,R-L+1);
            R++;
        }
        return ret;
    }
}
